﻿using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Cnm.Core.Caching;
using Cnm.Models;
using Cnm.Models.ViewModels;
using Cnm.Servicers.Seo;

namespace Cnm.Servicers.Cacher
{
    public class Cacher : ICacher
    {
        private ICacheManager _cacheManager;
        private readonly CnmDemoEntities dbContext = new CnmDemoEntities();

        public Cacher(ICacheManager cacheManager)
        {
            this._cacheManager = cacheManager;
        }

        public List<CategoryViewModel> GetMenuModels()
        {
            string key = string.Format("MenuModels");
            var models = _cacheManager.Get(key, () =>
            {
                var allMenus = GetAllCategoryViewModels();
                var first = allMenus.Where(p => p.ParentId == null).ToList();
                for (int i = 0; i < first.Count(); i++)
                {
                    first[i].Childs.AddRange(allMenus.Where(p => p.ParentId == first[i].Id));
                }
                return first;
            });

            return models;
        }

        public List<CategoryViewModel> GetAllCategoryViewModels()
        {
            string key = string.Format("AllCategoryViewModels");
            var models = _cacheManager.Get(key, () => dbContext
                .Categories
                .ToList()
                .Select(p => new CategoryViewModel
                {
                    Id = p.Id,
                    Name = p.Name,
                    DisplayOrder = p.DisplayOrder,
                    ParentId = p.ParentId,
                    Type = p.Type,
                    SeName = p.GetSeName()
                })
                .ToList());

            return models;
        }

        public List<RefData> GetAllLevelCategories()
        {
            var key = "AllLevelCategories";
            var models = _cacheManager.Get(key, () =>
            {
                var categories = dbContext
                    .Categories
                    //.ToList()
                    .Select(p => new CategoryViewModel
                    {
                        Id = p.Id,
                        Name = p.Name,
                        //DisplayOrder = p.DisplayOrder,
                        ParentId = p.ParentId,
                        //Type = p.Type,
                        //SeName = p.GetSeName()
                    })
                    .ToList();
                foreach (var categoryViewModel in categories)
                {
                    if (categoryViewModel.ParentId != null)
                    {
                        var parentCat = categories.FirstOrDefault(p => p.Id == categoryViewModel.ParentId);
                        if (parentCat != null)
                            categoryViewModel.Name =
                                parentCat.Name
                                + " » "
                                + categoryViewModel.Name;
                    }
                }

                return categories
                    .Select(p => new RefData
                    {
                        Key = p.Id.ToString(CultureInfo.InvariantCulture),
                        Text = p.Name
                    })
                    //.OrderBy(p => p.Text)
                    .ToList();
            });

            return models;
        }

        public List<RefData> GetFirstLevelCategories()
        {
            string key = string.Format("FirstLevelCategories");
            var models = _cacheManager.Get(key, () =>
            {
                return GetAllCategoryViewModels()
                    .Where(p => p.ParentId == null)
                    .ToList()
                    .Select(p => new RefData
                    {
                        Key = p.Id.ToString(CultureInfo.InvariantCulture),
                        Text = p.Name
                    }).ToList();
            });

            return models;
        }

        public List<RefData> GetSecondLevelCategories(int parentId)
        {
            string key = string.Format("SecondLevelCategories.ParentId-{0}", parentId);
            var models = _cacheManager.Get(key, () =>
            {
                return GetAllCategoryViewModels()
                .Where(p => p.ParentId == parentId)
                .ToList()
                .Select(p => new RefData
                {
                    Key = p.Id.ToString(CultureInfo.InvariantCulture),
                    Text = p.Name
                }).ToList();
            });

            return models;
        }

        public List<RefData> GetRegions()
        {
            string key = string.Format("Regions");
            var models = _cacheManager.Get(key, () =>
            {
                return dbContext
                .Regions
                .ToList()
                .Select(p => new RefData
                {
                    Key = p.Id.ToString(CultureInfo.InvariantCulture),
                    Text = p.Name
                }).ToList();
            });

            return models;
        }

        public List<RefData> GetItemStatus()
        {
            string key = string.Format("ItemStatus");
            var models = _cacheManager.Get(key, () =>
            {
                return dbContext
                .ItemStatus
                .ToList()
                .Select(p => new RefData
                {
                    Key = p.Id.ToString(CultureInfo.InvariantCulture),
                    Text = p.Name
                });
            }).ToList();

            return models;
        }

        public List<RefData> GetPaymentMethods()
        {
            string key = string.Format("PaymentMethods");
            var models = _cacheManager.Get(key, () =>
            {
                return dbContext
                .PaymentMethods
                .ToList()
                .Select(p => new RefData
                {
                    Key = p.Id.ToString(),
                    Text = p.Name
                }).ToList();
            });

            return models;
        }

        public List<RefData> GetPostTypes(int? categoryId)
        {
            var type = 1; // Default
            var cat = GetAllCategoryViewModels().FirstOrDefault(p => p.Id == categoryId);
            if (cat != null && cat.Type != null)
                type = (int)cat.Type;

            string key = string.Format("PostTypes.Type-{0}", type);
            var models = _cacheManager.Get(key, () =>
            {
                return dbContext
                    .PostTypes
                    .Where(p=>p.Type == type)
                    .ToList()
                    .Select(p => new RefData
                    {
                        Key = p.Id.ToString(CultureInfo.InvariantCulture),
                        Text = p.Name
                    })
                    .ToList();
            });

            return models;
        }

        public List<RefData> GetTitulars()
        {
            string key = string.Format("Titulars");
            var models = _cacheManager.Get(key, () =>
            {
                return dbContext
                    .Titulars
                    .ToList()
                    .Select(p => new RefData
                    {
                        Key = p.Id.ToString(CultureInfo.InvariantCulture),
                        Text = p.Name
                    })
                    .ToList();
            });

            return models;
        }
    }
}